package com.ruoyi.web.controller.campus;

import com.ruoyi.campus.domain.MyOrder;
import com.ruoyi.campus.service.IMyOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 个人订单Controller
 *
 * @author zht
 * @date 2021-10-24
 */
@RestController
@RequestMapping("/campus/userOrder")
public class MyUserOrderController extends BaseController
{
    @Autowired
    private IMyOrderService myOrderService;

    /**
     * 查询个人订单列表
     */
    @PreAuthorize("@ss.hasPermi('campus:userOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyOrder myOrder)
    {
        startPage();
        List<MyOrder> list = myOrderService.selectMyOrderListByUserId(myOrder,getUserId());
        return getDataTable(list);
    }

    /**
     * 查询个人订单(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/selectMyOrder")
    public TableDataInfo selectMyOrder(@RequestParam("userId") String userId,@RequestParam("orderStatus") String orderStatus){
        startPage();
        List<Map<String,Object>> list = myOrderService.selectMyOrder(userId,orderStatus);
        for (Map<String, Object> map : list) {
            String soldMoney = map.get("soldMoney").toString();
            String oriMoney = map.get("oriMoney").toString();
            map.put("soldMoney",soldMoney);
            map.put("oriMoney",oriMoney);
        }
        return getDataTable(list);
    }

    /**
     * 评论订单(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/evaluateOrder")
    public AjaxResult evaluateOrder(@RequestParam("orderId") String orderId, @RequestParam("evaluate") String evaluate){
        return toAjax(myOrderService.evaluateOrderById(orderId,evaluate));
    }

    /**
     * 创建订单(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody MyOrder myOrder){
        myOrder.setOrderStatus("0");
        myOrder.setOrderId(UUID.randomUUID().toString().replace("-",""));
        if (myOrderService.insertMyOrder(myOrder) > 0){
            return toAjax(myOrderService.updateCommStatus(myOrder.getComId()));
        }else{
            return error();
        }
    }

    /**
     * 取消订单(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/cancelOrder")
    public AjaxResult cancelOrder(@RequestParam("orderId") String orderId, @RequestParam("comId") String comId){
        return toAjax(myOrderService.cancelOrder(orderId,comId));
    }

    /**
     * 确认订单(小程序端接口)
     */
    @PreAuthorize("@ss.hasAnyRoles('Administrator,member')")
    @GetMapping("/finishOrder")
    public AjaxResult finishOrder(@RequestParam("orderId") String orderId){
        return toAjax(myOrderService.finishOrder(orderId));
    }

    /**
     * 获取个人订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:userOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return AjaxResult.success(myOrderService.selectMyOrderByOrderId(orderId));
    }

    /**
     * 新增个人订单
     */
    @PreAuthorize("@ss.hasPermi('campus:userOrder:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyOrder myOrder)
    {
        myOrder.setOrderId(UUID.randomUUID().toString().replace("-",""));
        return toAjax(myOrderService.insertMyOrder(myOrder));
    }

    /**
     * 修改个人订单
     */
    @PreAuthorize("@ss.hasPermi('campus:userOrder:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyOrder myOrder)
    {
        return toAjax(myOrderService.updateMyOrder(myOrder));
    }

    /**
     * 删除个人订单
     */
    @PreAuthorize("@ss.hasPermi('campus:userOrder:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(myOrderService.deleteMyOrderByOrderIds(orderIds));
    }
}
